package com.oim.fx.ui.component;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 * @author: XiaHui
 * @date: 2017年6月1日 下午3:01:06
 */
public class ImageStyleButton extends Button {

	ImageView imageView = new ImageView();

	private Image normalImage = null;
	private Image hoverImage = null;
	private Image pressedImage = null;

	public ImageStyleButton() {
		initIconStyleButton();
		initEvent();
	}

	public ImageStyleButton(Image normalImage, Image hoverImage, Image pressedImage) {
		this.normalImage = normalImage;
		this.hoverImage = hoverImage;
		this.pressedImage = pressedImage;
		this.setNormalImage(normalImage);
		initIconStyleButton();
		initEvent();
	}

	private void initIconStyleButton() {
		this.setFocusTraversable(false);
		// this.setGraphicTextGap(1);
		this.setPadding(new Insets(3, 6, 3, 6));
		this.getStyleClass().remove("button");
		this.getStyleClass().add("icon-1-button");
		this.setGraphic(imageView);

	}

	private void initEvent() {

		this.hoverProperty().addListener(h -> {
			boolean hover = this.hoverProperty().getValue();
			if (null != hoverImage && hover) {
				imageView.setImage(hoverImage);
				this.setStyle("-fx-background-color:rgba(160, 160, 160, 0.5);");
			} else {
				imageView.setImage(normalImage);
				this.setStyle("-fx-background-color:null;");
			}
		});

		this.pressedProperty().addListener(p -> {
			boolean pressed = this.hoverProperty().getValue();
			if (null != pressedImage && pressed) {
				imageView.setImage(pressedImage);
				this.setStyle("-fx-background-color:rgba(120, 120, 120, 0.5);");
			} else {
				imageView.setImage(normalImage);
				this.setStyle("-fx-background-color:null;");
			}
		});
	}

	public void setNormalImage(Image value) {
		normalImage = value;
		imageView.setImage(value);
	}

	public void setImageSize(double width, double height) {
		imageView.setFitHeight(height);
		imageView.setFitWidth(width);
	}

	public void setImageFitWidth(double value) {
		imageView.setFitWidth(value);
	}

	public void setImageFitHeight(double value) {
		imageView.setFitHeight(value);
	}
}
